﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KPIS.GERP.GHRMS.APP.Classes;

namespace KPIS.GERP.GHRMS.APP.App_UI.Forms
{
    public partial class PS_HisPrivateEditPicture : System.Web.UI.Page
    {
        private const string fileDirectory = "../../OtherImages/Person";
        private const string fileImageNoImage = "../../App_UI/Images/Buttons/No%20Picture.jpg";

        private Main main = new Main();

        protected void Page_Load(object sender, EventArgs e)
        {
            lbtFileName.Attributes.Add("onClick", "return false;");

            if (!IsPostBack)
            {
                string filename = Request["filename"];
                hdfFileName.Value = filename;
                lbtFileName.Text = filename;

                if (!String.IsNullOrEmpty(hdfFileName.Value) &&
                    File.Exists(GetImageFullPath(hdfFileName.Value)))
                {
                    previewImage.ImageUrl = GetImageUrl(hdfFileName.Value);
                }
                else
                {
                    previewImage.ImageUrl = fileImageNoImage;
                }
            }
        }

        protected void btnUploadPicture_Click(object sender, EventArgs e)
        {
            var fileContent = fileUploader.FileContent;
            if (fileContent != null)
            {
                lblFileTempPath.Text = UploadToTempImage(fileContent);
                hdfFileName.Value = lblFileTempPath.Text;
                lbtFileName.Text = lblFileTempPath.Text;
                // imgPhoto.ImageUrl = GetImageUrl(lblFileTempPath.Text);
                previewImage.ImageUrl = GetImageUrl(lblFileTempPath.Text);
            }
        }

        protected void btnClearImage_Click(object sender, EventArgs e)
        {
            try
            {
                lblFileTempPath.Text = String.Empty;
                hdfFileName.Value = String.Empty;
                lbtFileName.Text = String.Empty;
                // imgPhoto.ImageUrl = GetImageUrl("No Picture.jpg");
                previewImage.ImageUrl = fileImageNoImage;
                // GetImageUrl("No Picture.jpg");
            }
            catch (Exception error)
            {

            }
        }

        private string UploadToTempImage(Stream fileContent)
        {
            string dirFullPath = HttpContext.Current.Server.MapPath(fileDirectory);

            if (!Directory.Exists(dirFullPath))
                Directory.CreateDirectory(dirFullPath);

            if (!Directory.Exists(dirFullPath))
                throw new Exception(String.Format("Can't create directory at \"{0}\"", fileDirectory));

            string fileName = main.DateTimeNumber + ".jpg";
            SaveStreamToFile(GetImageFullPath(fileName), fileContent);
            return fileName;
        }

        public void SaveStreamToFile(string fileFullPath, Stream stream)
        {
            if (stream.Length == 0) return;

            // Create a FileStream object to write a stream to a file
            using (FileStream fileStream = File.Create(fileFullPath, (int)stream.Length))
            {
                // Fill the bytes[] array with the stream data
                byte[] bytesInStream = new byte[stream.Length];
                stream.Read(bytesInStream, 0, (int)bytesInStream.Length);

                // Use FileStream object to write to the specified file
                fileStream.Write(bytesInStream, 0, bytesInStream.Length);
            }
        }

        private string GetImageUrl(string imageFileName)
        {
            return "~/OtherImages/Person/" + imageFileName;
        }

        private string GetImageFullPath(string imageFileName)
        {
            return Server.MapPath("~/OtherImages/Person/" + imageFileName);
        }

    }
}